package cn.zhoutao.basic.controller;

import cn.zhoutao.basic.dto.LoginDto;
import cn.zhoutao.basic.dto.WeChatDto;
import cn.zhoutao.basic.exception.BusinessException;
import cn.zhoutao.basic.service.ILoginService;
import cn.zhoutao.basic.util.AjaxResult;
import cn.zhoutao.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author:MrZhou
 * @Date:2022/6/18 17:56
 *
 * 统一登录接口
 * 三方登陆接口
 *
 * 手机，邮箱随机码登陆接口
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    /**
     * 账号密码登录
     * @param dto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            //登陆成功之后要返回token和logininfo
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存店铺失败！"+e.getMessage());
        }
    }

    /**
     * 微信扫码登录
     * @param dto
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WeChatDto dto){
        try {
            return loginService.wechat(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存店铺失败！"+e.getMessage());
        }
    }

    /**
     * 微信绑定接口
     * @param dto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WeChatDto dto){
        try {
            //登陆成功之后要返回token和logininfo
            Map<String, Object> map = loginService.binder(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存店铺失败！"+e.getMessage());
        }
    }
}
